/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.icq.accounts;

import ru.icq.io.connections.ProtocolType;
import ru.icq.io.connections.PresenceJICQ;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.UUID;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JPopupMenu.Separator;
import org.apache.log4j.Logger;
import ru.icq.io.connections.ConnectionsCustom;
import ru.icq.io.loadTheme;
import ru.icq.listener.IAccountListener;

/**
 * Настройки акаунта.
 * Возможно надо создать всякие jabberAccount, icqAccount и т.п.
 * оставив в general всю дрянь, в остальное перенасти только меню. Оно вроде 
 * как разное у всех.
 * @author Носов А.В.
 */
public class GeneralAccountjICQ extends JButton implements IAccountListener {

    // Variables declaration
    private static final Logger log = Logger.getLogger(GeneralAccountjICQ.class);
    
    /** Тема */
    private loadTheme theme = new loadTheme("Default", "Russian");
    private String id = UUID.randomUUID().toString();
    
    private AccountInfo info;
    private ConnectionsCustom connection;
    
    JPopupMenu popupStatus;
    JMenuItem statusItemOnLine;
    JMenuItem statusItemFreeForChat;
    JMenuItem statusItemAway;
    JMenuItem statusItemDoNotDistrub;
    JMenuItem statusItemNotAvailable;
    JMenuItem statusItemInvisible;
    JMenuItem statusItemOffLine;
    Separator separatorStatus1;
    JMenuItem statusItemSetStatus;
    // End of variables declaration

    public GeneralAccountjICQ(AccountInfo info) {
        this.setToolTipText(info.getUid());
        this.info = info;
        
        initComponents();
        connection = new ConnectionsCustom(info);
        connection.setID(id);
        connection.addListener(this);
    }

    private void initComponents() {
        popupStatus = new JPopupMenu();
        statusItemOnLine = new JMenuItem();
        statusItemFreeForChat = new JMenuItem();
        statusItemAway = new JMenuItem();
        statusItemDoNotDistrub = new JMenuItem();
        statusItemNotAvailable = new JMenuItem();
        statusItemInvisible = new JMenuItem();
        statusItemOffLine = new JMenuItem();
        separatorStatus1 = new Separator();
        statusItemSetStatus = new JMenuItem();
        
        statusItemOnLine.setText("On line");
        statusItemOnLine.setName("status" + info.getProtocolType().name() + ".onLine");
        statusItemOnLine.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemOnLineActionPerformed(evt);
            }
        });
        
        statusItemFreeForChat.setText("Free For Chat");
        statusItemFreeForChat.setName("status" + info.getProtocolType().name() + ".freeForChat");
        statusItemFreeForChat.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemFreeForChatActionPerformed(evt);
            }
        });
        
        statusItemAway.setText("Away");
        statusItemAway.setName("status" + info.getProtocolType().name() + ".away");
        statusItemAway.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemAwayActionPerformed(evt);
            }
        });
        
        statusItemDoNotDistrub.setText("Do Not Distrub");
        statusItemDoNotDistrub.setName("status" + info.getProtocolType().name() + ".doNotDistrub");
        statusItemDoNotDistrub.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemDoNotDistrubActionPerformed(evt);
            }
        });
        
        statusItemNotAvailable.setText("NotAvailable");
        statusItemNotAvailable.setName("status" + info.getProtocolType().name() + ".notAvailable");
        statusItemNotAvailable.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemNotAvailableActionPerformed(evt);
            }
        });
        
        statusItemInvisible.setText("Invisible");
        statusItemInvisible.setName("status" + info.getProtocolType().name() + ".invisible");
        statusItemInvisible.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemInvisibleActionPerformed(evt);
            }
        });
        
        statusItemOffLine.setText("off-line");
        statusItemOffLine.setName("status" + info.getProtocolType().name() + ".offLine");
        statusItemOffLine.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemOffLineActionPerformed(evt);
            }
        });
        
        statusItemSetStatus.setText("Set Status Text");
        statusItemSetStatus.setName("status" + info.getProtocolType().name() + ".setStatus");
        statusItemSetStatus.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemSetStatusActionPerformed(evt);
            }
        });
        
        popupStatus.add(statusItemOnLine);
        popupStatus.add(statusItemFreeForChat);
        popupStatus.add(statusItemAway);
        popupStatus.add(statusItemDoNotDistrub);
        popupStatus.add(statusItemNotAvailable);
        popupStatus.add(statusItemInvisible);
        popupStatus.add(statusItemOffLine);
        popupStatus.add(separatorStatus1);
        popupStatus.add(statusItemSetStatus);
        
        this.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popupMenuActionPerformed(evt);
            }
        });
    }

    /**
     * Возвращает идентификатор соединения.
     * @return мдентификатор
     */
    public String getID() {
        return id;
    }

    /**
     * Возвращает идентификатор аккаунта.
     * @return идентификатор
     */
    public String getUid() {
        return info.getUid();
    }

    /**
     * Устанавливает тему.
     * @param theme тема
     */
    public void setTheme(loadTheme theme) {
        if (theme != null) this.theme = theme;
        this.theme.setTheme( (Component)popupStatus );
        switch (info.getProtocolType()) {
            case ICQ:
                this.setIcon(this.theme.getIcon("account.icq"));
                break;
            case JABBER:
                this.setIcon(this.theme.getIcon("account.jabber"));
                break;
            case VKONTAKTE:
                this.setIcon(this.theme.getIcon("account.vkontakte"));
                break;
        }
    }


    private void popupMenuActionPerformed(ActionEvent evt) {
        popupStatus.show(this, this.getX(), this.getY()+this.getWidth());
    }

    /**
     * Добавление слушателя на события смены статуса соединения.
     * @param l слушатель.
     */
    public void AddStatusConnection(IAccountListener l) {
        connection.addListener(l);
    }

    private void menuItemOnLineActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.ON_LINE);
    }

    private void menuItemFreeForChatActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.FREE_FOR_CHAT);
    }

    private void menuItemAwayActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.AWAY);
    }

    private void menuItemDoNotDistrubActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.DO_NOT_DIDTRUB);
    }

    private void menuItemNotAvailableActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.NOT_AVAILABLE);
    }

    private void menuItemInvisibleActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.INVISIBLE);
    }

    public void menuItemOffLineActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        connection.setPresenceJICQ(PresenceJICQ.OFF_LINE);
//        connection.disconnect();
    }

    private void menuItemSetStatusActionPerformed(ActionEvent evt) {
        if (connection == null) return;
        //connection.setPresenceJICQ(Mode.xa);
        log.info("Не используется");
    }

    @Override
    public void changePresence(ConnectionsCustom connection) {
        switch (connection.getPresenceJICQ()) {
            case AWAY:
                setIcon(statusItemAway.getIcon());
                break;
            case DO_NOT_DIDTRUB:
                setIcon(statusItemDoNotDistrub.getIcon());
                break;
            case FREE_FOR_CHAT:
                setIcon(statusItemFreeForChat.getIcon());
                break;
            case INVISIBLE:
                setIcon(statusItemInvisible.getIcon());
                break;
            case NOT_AVAILABLE:
                setIcon(statusItemNotAvailable.getIcon());
                break;
            case OFF_LINE:
                setIcon(statusItemOffLine.getIcon());
                break;
            case ON_LINE:
                setIcon(statusItemOnLine.getIcon());
                break;
            default:
                setIcon(statusItemOffLine.getIcon());
        }
    }

    @Override
    public void processChatMessage(String from, String msg) {
    }

    @Override
    public String toString() {
        return info.getUid();
    }
}
